package com.kintreda.ieco_server.module.autoreply.web;

import com.kintreda.ieco_server.bean.account.User;
import com.kintreda.ieco_server.bean.massage.Massage;
import com.kintreda.ieco_server.bean.system.ArtificialReply;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.api.service.IMassageService;
import com.kintreda.ieco_server.module.autoreply.service.IArtificialReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.autoreply.web
 * @Author ZhongYao_H
 * @Date 2019/5/21:19:17
 * @Year 2019 Month 05 Day 21
 * @Date_zh 2019 年 五月 星期二
 */
@Controller
@RequestMapping("/artificialreply")
public class ArtificialReplyController {

    @Autowired
    IArtificialReplyService artificialReplyService;
    @Autowired
    IMassageService massageService;
    @Autowired
    IUserService userService;

    @RequestMapping("/list")
    public ModelAndView citylist(){
        return new ModelAndView("artificialreply/artificialreply-list");
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object query(){
            return artificialReplyService.query("from ArtificialReply where status = 0 ",null);
    }

    @RequestMapping("/add")
    public ModelAndView add( String userId,String id){
        ModelAndView andView = new ModelAndView("artificialreply/artificialreply-add");
        andView.addObject("id",id);
        andView.addObject("userId",userId);
        return andView;
    }

    @RequestMapping("/searchreply")
    public ModelAndView searchreply( String moblie){
        return new ModelAndView("artificialreply/search-reply").addObject("moblie",moblie);

    }


    @RequestMapping(value = "/searchreply",method = RequestMethod.POST)
    @ResponseBody
    public Object searchreply( String msg,String title,String mobile){
        User user = userService.queryByMobile(mobile);
        if (user==null){
            return R.error("手机号未注册!");
        }
        Massage massage = new Massage();
        massage.setDestination(user.getId());
        massage.setTitle(title);
        massage.setStatus("0");//未读
        massage.setBody(msg);

        try{
            massageService.save(massage);
        }catch (Exception e){
            return R.error("回复内容过长");
        }


        return R.ok("发送成功");
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(String msg,String title,String userId,String id){
        ArtificialReply artificialReply = artificialReplyService.get(id);
        artificialReply.setStatus("1");
        Massage massage = new Massage();
        massage.setDestination(userId);
        massage.setTitle(title);
        massage.setStatus("0");//未读
        massage.setBody(msg);
        massageService.save(massage);
        artificialReplyService.update(artificialReply);
        return R.ok("回复成功");

    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(String id){
        ArtificialReply artificialReply = artificialReplyService.get(id);
        artificialReply.setStatus("1");
        artificialReplyService.update(artificialReply);
        return R.ok("操作成功!");
    }


}
